import { getSliderValue } from "./displayInfor.js";

export function stopByTimeBuyOrSell() {
  const btn = document.querySelector(".trading_sub > .mantine-1v71nuo");
  console.log(btn, "btn");
  if (btn) {
    const originalHandler = btn.onclick;
    if (originalHandler) {
      btn.onclick = null;
    }
    btn.addEventListener("click", async (event) => {
      const sliderValue = getSliderValue();
      const inputValue = getInputValue();
      console.log(sliderValue, inputValue, "inputValue");

      console.log("买入按钮被点击");
      event.preventDefault();
      console.log("执行自定义逻辑");
      // chrome.runtime.sendMessage(
      //   {
      //     action: "interceptRequest",
      //     url: "https://www.gate.io/apiw/v2/unified/futures/usdt/contracts",
      //   },
      //   (response) => {
      //     if (chrome.runtime.lastError) {
      //       console.error("消息发送失败：", chrome.runtime.lastError.message);
      //     } else {
      //       console.log("收到响应：", response);
      //     }
      //   }
      // );
    });
  } else {
    console.log("未找到.trading_sub > .mantine-1v71nuo买入按钮节点");
  }
}

function getInputValue() {
  const params = {};
  const element = document.querySelector(
    '.mantine-17wb7nx span[style*="transform"]'
  ); // 查找包含 transform 样式的 span 元素
  console.log(element, "advancedOptions");
  if (element) {
    const rotateValue = element.style.transform
      .split("rotate(")[1]
      .split("deg)")[0];
    console.log(rotateValue, "rotateValue");
    if (rotateValue == "180") {
      const timeInputGroup = document.querySelectorAll(
        ".mantine-1i52ecs > input"
      );
      params.total = timeInputGroup[0].value;
      params.timeHours = timeInputGroup[1].value;
      params.timeMinutes = timeInputGroup[2].value;
      params.proportion = timeInputGroup[3].value;
      params.activatePrice = timeInputGroup[4].value;
      params.timeInterval = timeInputGroup[5].value;
      params.quantityPer = timeInputGroup[6].value;
    } else {
      const timeInputGroupZero = document.querySelectorAll(
        ".mantine-1i52ecs > input"
      );
      params.total = timeInputGroupZero[0].value;
      params.timeHours = timeInputGroupZero[1].value;
      params.timeMinutes = timeInputGroupZero[2].value;
    }
    return params;
  } else {
    console.log("未找到包含 transform 样式的元素");
  }
  return params;
}
